package com.hycreate.platform.modules.pointunit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;

/**
 * 积分记录Controller
 * @author 王
 * @version 2017-11-29
 */
@Controller
@RequestMapping(value = "${adminPath}/pointunit/yhcgPointunit")
public class YhcgPointunitController extends BaseController {

	@Autowired
	private YhcgPointunitService yhcgPointunitService;
	
	@ModelAttribute
	public YhcgPointunit get(@RequestParam(required=false) String id) {
		YhcgPointunit entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yhcgPointunitService.get(id);
		}
		if (entity == null){
			entity = new YhcgPointunit();
		}
		return entity;
	}
	
	@RequiresPermissions("pointunit:yhcgPointunit:view")
	@RequestMapping(value = {"list", ""})
	public String list(YhcgPointunit yhcgPointunit, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<YhcgPointunit> page = yhcgPointunitService.findPage(new Page<YhcgPointunit>(request, response), yhcgPointunit); 
		model.addAttribute("page", page);
		return "modules/pointunit/yhcgPointunitList";
	}

	@RequiresPermissions("pointunit:yhcgPointunit:view")
	@RequestMapping(value = "form")
	public String form(YhcgPointunit yhcgPointunit, Model model) {
		model.addAttribute("yhcgPointunit", yhcgPointunit);
		return "modules/pointunit/yhcgPointunitForm";
	}

	@RequiresPermissions("pointunit:yhcgPointunit:edit")
	@RequestMapping(value = "save")
	public String save(YhcgPointunit yhcgPointunit, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, yhcgPointunit)){
			return form(yhcgPointunit, model);
		}
		yhcgPointunitService.save(yhcgPointunit);
		addMessage(redirectAttributes, "保存积分记录成功");
		return "redirect:"+Global.getAdminPath()+"/pointunit/yhcgPointunit/?repage";
	}
	
	@RequiresPermissions("pointunit:yhcgPointunit:edit")
	@RequestMapping(value = "delete")
	public String delete(YhcgPointunit yhcgPointunit, RedirectAttributes redirectAttributes) {
		yhcgPointunitService.delete(yhcgPointunit);
		addMessage(redirectAttributes, "删除积分记录成功");
		return "redirect:"+Global.getAdminPath()+"/pointunit/yhcgPointunit/?repage";
	}

}